package com.rizhov.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.rizhov.enumerations.GraphOrientation;
import com.rizhov.enumerations.IteratorKind;
import com.rizhov.graph.Graph;
import com.rizhov.iterators.GoingOutEdgeIterator;
import com.rizhov.iterators.GraphEdgeIterator;
import com.rizhov.iterators.GraphIterator;
import com.rizhov.iterators.IncomingEdgeIterator;
import com.rizhov.iterators.VertexIterator;
import com.rizhov.tasks.FirstTask;
import com.rizhov.tasks.SecondTask;
import com.rizhov.tasks.Task;

public class MainWindow extends JFrame {

    public static StatisticTextArea textArea;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
	    @Override
	    public void run() {
		try {
		    MainWindow frame = new MainWindow();
		    frame.setVisible(true);
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	});
    }

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JComboBox comboBox;
    private JButton btnBegin;
    private JButton btnNext;
    private JButton btnEnd;
    private JButton btnChange;

    /** current iterator **/
    public GraphIterator iterator = new VertexIterator();

    /** current task **/
    public Task task = new FirstTask(Graph.getInstance());;

    /** entered index in dialge window **/
    public static int selectedVertexIndex = 0;

    /** marked as special edges **/
    public static int iteratorStartIndex = -1;

    /** marked as special edges **/
    public static int iteratorEndIndex = -1;

    private JTextField textField_2;
    private JTextField textField_3;
    private JComboBox comboBox_1;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;

    /**
     * Create the frame.
     * 
     * @throws IOException
     */
    public MainWindow() throws IOException {
	try {
	    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		// System.out.println(info.getName());
		if ("Windows Classic".equals(info.getName())) {
		    UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	} catch (Exception e) {
	    // If Windows Classic is not available, you can set the GUI to
	    // another look
	    // but I won't do it, yeah i'm crazy
	}

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(0, 0, 1340, 723);
	contentPane = new JPanel();

	JMenuBar menuBar = new JMenuBar();
	setJMenuBar(menuBar);

	JMenu mnFile = new JMenu("File");
	menuBar.add(mnFile);

	JMenu mnHelp = new JMenu("Help");
	menuBar.add(mnHelp);
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setLayout(null);

	JPanel layoutPanelBorder = new JPanel();
	layoutPanelBorder.setBorder(new TitledBorder(null, "Layout", TitledBorder.LEADING,
		TitledBorder.TOP, null, null));
	layoutPanelBorder.setBounds(10, 11, 491, 568);
	contentPane.add(layoutPanelBorder);

	layoutPanelBorder.setLayout(new BorderLayout(0, 0));

	JPanel layoutPanel = LayoutPanel.getInstance();
	layoutPanel.setBackground(Color.WHITE);
	layoutPanelBorder.add(layoutPanel, BorderLayout.CENTER);

	JPanel menuPanel = new JPanel();
	menuPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
		"Menu", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
	menuPanel.setBounds(511, 11, 235, 653);
	contentPane.add(menuPanel);
	menuPanel.setLayout(null);

	JPanel panel = new JPanel();
	panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
		"Graph direction type", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,
			0, 0)));
	panel.setBounds(21, 74, 201, 79);
	menuPanel.add(panel);
	panel.setLayout(null);

	ButtonGroup bg1 = new ButtonGroup();
	final JRadioButton rdbtnDirectedGraph = new JRadioButton("Oriented");
	rdbtnDirectedGraph.setSelected(true);
	rdbtnDirectedGraph.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Graph.getInstance().setGraphOrientation(GraphOrientation.ORIENTED_GRAPH);
		repaint();
	    }
	});
	bg1.add(rdbtnDirectedGraph);
	rdbtnDirectedGraph.setBounds(6, 23, 109, 23);
	panel.add(rdbtnDirectedGraph);

	final JRadioButton rdbtnUndirected = new JRadioButton("Unoriented");
	rdbtnUndirected.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Graph.getInstance().setGraphOrientation(GraphOrientation.UNORIENTED_GRAPH);
		repaint();
	    }
	});
	bg1.add(rdbtnUndirected);
	rdbtnUndirected.setBounds(6, 49, 109, 23);
	panel.add(rdbtnUndirected);

	JPanel panel_1 = new JPanel();
	panel_1.setBorder(new TitledBorder(null, "Graph model", TitledBorder.LEADING,
		TitledBorder.TOP, null, null));
	panel_1.setBounds(21, 164, 201, 79);
	menuPanel.add(panel_1);
	panel_1.setLayout(null);

	ButtonGroup bg2 = new ButtonGroup();
	final JRadioButton rdbtnMatrix = new JRadioButton("Matrix");
	rdbtnMatrix.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Graph.getInstance().convertToMatrixGraph();
		LayoutPanel.getInstance().repaint();
	    }
	});
	bg2.add(rdbtnMatrix);
	rdbtnMatrix.setBounds(6, 19, 109, 23);
	panel_1.add(rdbtnMatrix);

	final JRadioButton rdbtnList = new JRadioButton("List");
	rdbtnList.setSelected(true);
	rdbtnList.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Graph.getInstance().convertToListGraph();
		LayoutPanel.getInstance().repaint();
	    }
	});
	bg2.add(rdbtnList);
	rdbtnList.setBounds(6, 45, 109, 23);
	panel_1.add(rdbtnList);

	JPanel panel_3 = new JPanel();
	panel_3.setBorder(new TitledBorder(null, "Iterator", TitledBorder.LEADING,
		TitledBorder.TOP, null, null));
	panel_3.setBounds(21, 254, 204, 243);
	menuPanel.add(panel_3);
	panel_3.setLayout(null);

	String[] comboElements = { "VERTEX_ITERATOR", "GRAPH_EDGE_ITERATOR",
		"GOING_OUT_EDGE_ITERATOR", "INCOMING_EDGE_ITERATOR" };
	comboBox = new JComboBox(comboElements);
	comboBox.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		comboBoxStateChanged();
	    }
	});
	comboBox.setBounds(10, 40, 181, 22);
	panel_3.add(comboBox);

	btnBegin = new JButton("begin");
	btnBegin.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		String choice = (String) comboBox.getSelectedItem();
		if (choice.equals(IteratorKind.VERTEX_ITERATOR.toString())) {
		    iterator.setToBegin();
		    Object data = iterator.getData();
		    if (data != null) {
			textField.setText(data.toString());
		    } else {
			textField.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GRAPH_EDGE_ITERATOR.toString())) {
		    iterator.setToBegin();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GOING_OUT_EDGE_ITERATOR.toString())) {
		    iterator.setToBegin();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.INCOMING_EDGE_ITERATOR.toString())) {
		    iterator.setToBegin();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		}
		LayoutPanel.getInstance().repaint();
		repaint();
	    }
	});
	btnBegin.setBounds(10, 73, 61, 23);
	panel_3.add(btnBegin);

	btnEnd = new JButton("end");
	btnEnd.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		String choice = (String) comboBox.getSelectedItem();
		if (choice.equals(IteratorKind.VERTEX_ITERATOR.toString())) {
		    iterator.setToEnd();
		    Object data = iterator.getData();
		    if (data != null) {
			textField.setText(data.toString());
		    } else {
			textField.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GRAPH_EDGE_ITERATOR.toString())) {
		    iterator.setToEnd();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GOING_OUT_EDGE_ITERATOR.toString())) {
		    iterator.setToEnd();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.INCOMING_EDGE_ITERATOR.toString())) {
		    iterator.setToEnd();
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		}
		LayoutPanel.getInstance().repaint();
		repaint();
	    }
	});
	btnEnd.setBounds(138, 73, 53, 23);
	panel_3.add(btnEnd);

	btnNext = new JButton("next");
	btnNext.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		iterator.next();
		String choice = (String) comboBox.getSelectedItem();
		if (choice.equals(IteratorKind.VERTEX_ITERATOR.toString())) {
		    Object data = iterator.getData();
		    if (data != null) {
			textField.setText(data.toString());
		    } else {
			textField.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GRAPH_EDGE_ITERATOR.toString())) {
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.GOING_OUT_EDGE_ITERATOR.toString())) {
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		} else if (choice.equals(IteratorKind.INCOMING_EDGE_ITERATOR.toString())) {
		    if (iterator.getData() != null) {
			String[] strArr = (String[]) iterator.getData();
			textField.setText(strArr[0]);
			textField_3.setText(strArr[1]);
		    } else {
			textField.setText("null");
			textField_3.setText("null");
		    }
		}
		LayoutPanel.getInstance().repaint();
		repaint();
	    }
	});
	btnNext.setBounds(76, 73, 58, 23);
	panel_3.add(btnNext);

	JLabel lblIteratorKinde = new JLabel("Iterator kind:");
	lblIteratorKinde.setBounds(10, 22, 86, 14);
	panel_3.add(lblIteratorKinde);

	JLabel lblData = new JLabel("Data:");
	lblData.setBounds(10, 107, 46, 14);
	panel_3.add(lblData);

	textField = new JTextField();
	textField.setBounds(10, 121, 86, 20);
	panel_3.add(textField);
	textField.setColumns(10);

	JLabel lblNewLabel = new JLabel("Change (if it can be changed):");
	lblNewLabel.setBounds(10, 152, 181, 14);
	panel_3.add(lblNewLabel);

	textField_1 = new JTextField();
	textField_1.setBounds(10, 177, 86, 20);
	panel_3.add(textField_1);
	textField_1.setColumns(10);

	btnChange = new JButton("change");
	btnChange.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		String choice = (String) comboBox.getSelectedItem();
		if (choice.equals(IteratorKind.VERTEX_ITERATOR.toString())) {
		    String newData = textField_1.getText();
		    Object data = iterator.getData();
		    if (data != null) {
			LayoutPanel lp = LayoutPanel.getInstance();
			int resultIndex = 0;
			boolean alreadyFound = false;
			for (int i = 0; i < lp.ellipseArr.size(); i++) {
			    if (data.equals(lp.ellipseArr.get(i).getData().toString())) {
				resultIndex = i;
			    }
			    if (newData.equals(lp.ellipseArr.get(i).getData().toString())) {
				alreadyFound = true;
			    }
			}
			if (!alreadyFound) {
			    lp.ellipseArr.get(resultIndex).data = newData;
			    iterator.setData(newData);
			    textField.setText((String) iterator.getData());
			} else {
			    JOptionPane.showMessageDialog(null,
				    "That value is already exist in graph!", "Error!",
				    JOptionPane.ERROR_MESSAGE);
			}
		    }
		} else if (choice.equals(IteratorKind.GRAPH_EDGE_ITERATOR.toString())) {
		    String[] oldDataArr = (String[]) iterator.getData();
		    if (oldDataArr != null) {
			String[] newDataArr = new String[2];
			newDataArr[0] = textField_1.getText();
			newDataArr[1] = textField_2.getText();
			iterator.setData(newDataArr);
			textField.setText(newDataArr[0]);
			textField_3.setText(newDataArr[1]);
		    }
		} else if (choice.equals(IteratorKind.GOING_OUT_EDGE_ITERATOR.toString())) {
		    String[] oldDataArr = (String[]) iterator.getData();
		    if (oldDataArr != null) {
			String[] newDataArr = new String[2];
			newDataArr[0] = textField_1.getText();
			newDataArr[1] = textField_2.getText();
			iterator.setData(newDataArr);
			textField.setText(newDataArr[0]);
			textField_3.setText(newDataArr[1]);
		    }
		} else if (choice.equals(IteratorKind.INCOMING_EDGE_ITERATOR.toString())) {
		    String[] oldDataArr = (String[]) iterator.getData();
		    if (oldDataArr != null) {
			String[] newDataArr = new String[2];
			newDataArr[0] = textField_1.getText();
			newDataArr[1] = textField_2.getText();
			iterator.setData(newDataArr);
			textField.setText(newDataArr[0]);
			textField_3.setText(newDataArr[1]);
		    }
		}
		LayoutPanel.getInstance().repaint();
		MainWindow.this.repaint();
	    }
	});
	btnChange.setBounds(10, 208, 181, 23);
	panel_3.add(btnChange);

	textField_2 = new JTextField();
	textField_2.setBounds(105, 177, 86, 20);
	panel_3.add(textField_2);
	textField_2.setColumns(10);

	textField_3 = new JTextField();
	textField_3.setBounds(106, 121, 86, 20);
	panel_3.add(textField_3);
	textField_3.setColumns(10);

	JPanel panel_4 = new JPanel();
	panel_4.setBorder(new TitledBorder(null, "Task", TitledBorder.LEADING, TitledBorder.TOP,
		null, null));
	panel_4.setBounds(21, 498, 201, 144);
	menuPanel.add(panel_4);
	panel_4.setLayout(null);

	String[] choicesArr = new String[] { "FIRST_TASK", "SECOND_TASK" };
	comboBox_1 = new JComboBox(choicesArr);
	comboBox_1.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		if (comboBox_1.getSelectedItem().toString().equals("FIRST_TASK")) {
		    task = new FirstTask(Graph.getInstance());
		} else if (comboBox_1.getSelectedItem().toString().equals("SECOND_TASK")) {
		    task = new SecondTask(Graph.getInstance());
		}
	    }
	});
	comboBox_1.setBounds(10, 21, 181, 22);
	panel_4.add(comboBox_1);

	JButton btnCompute = new JButton("compute");
	btnCompute.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		task.restart();
		textField_4.setText(String.valueOf(task.getResult()));
	    }
	});
	btnCompute.setBounds(10, 54, 181, 23);
	panel_4.add(btnCompute);

	JLabel lblResult = new JLabel("Result:");
	lblResult.setBounds(10, 88, 46, 14);
	panel_4.add(lblResult);

	textField_4 = new JTextField();
	textField_4.setBounds(10, 113, 181, 20);
	panel_4.add(textField_4);
	textField_4.setColumns(10);

	JButton btnClear = new JButton("clear");
	btnClear.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Graph graph = Graph.getInstance();
		while (graph.getVertexCount() != 0) {
		    graph.removeVertex(graph.getVertexData(0));
		}
		LayoutPanel lp = LayoutPanel.getInstance();
		while (lp.ellipseArr.size() != 0) {
		    lp.ellipseArr.remove(0);
		}
		lp.repaint();
	    }
	});
	btnClear.setBounds(21, 40, 91, 23);
	menuPanel.add(btnClear);

	JButton btnRandom = new JButton("random");
	btnRandom.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Random random = new Random();
		LayoutPanel lp = LayoutPanel.getInstance();
		Graph graph = Graph.getInstance();
		int addedVertexCount = 0;
		while (addedVertexCount < 5) {
		    boolean successful = true;
		    int vertexDataTemp = random.nextInt(100);
		    for (int i = 0; i < lp.ellipseArr.size(); i++) {
			if (lp.ellipseArr.get(i).data.toString().equals(
				String.valueOf(vertexDataTemp))) {
			    successful = false;
			}
		    }
		    if (successful) {
			addedVertexCount++;
			lp.ellipseArr.add(new GraphVertexEllipse(
				random.nextInt(lp.getWidth() - 100) + 50, random.nextInt(lp
					.getHeight() - 100) + 50, LayoutPanel.DEFAULT_SIDE,
				LayoutPanel.DEFAULT_SIDE, String.valueOf(vertexDataTemp)));
			graph.addVertex(String.valueOf(vertexDataTemp));
		    }
		}
		int addedEdgesCount = 0;
		while (addedEdgesCount < 6) {
		    int fromIndex = random.nextInt(graph.getVertexCount());
		    int toIndex = random.nextInt(graph.getVertexCount());
		    if (fromIndex == toIndex || graph.isEdgeExist(fromIndex, toIndex)) {
			continue;
		    } else {
			addedEdgesCount++;
			graph.addEdge(fromIndex, toIndex, String.valueOf(random.nextInt(100)));
			graph.setEdgeWeight(fromIndex, toIndex, String.valueOf(random.nextInt(100)));
			graph.setEdgeData(fromIndex, toIndex, String.valueOf(random.nextInt(100)));
		    }
		}
		lp.repaint();
	    }
	});
	btnRandom.setBounds(131, 40, 91, 23);
	menuPanel.add(btnRandom);

	JPanel panel_2 = new JPanel();

	panel_2.setBounds(756, 11, 566, 653);
	panel_2.setBorder(new TitledBorder(null, "Statistics", TitledBorder.LEADING,
		TitledBorder.TOP, null, null));
	panel_2.setLayout(new BorderLayout(0, 0));
	textArea = new StatisticTextArea();
	JScrollPane sp = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
	panel_2.add(sp, BorderLayout.CENTER);
	contentPane.add(panel_2);

	JPanel panel_5 = new JPanel();
	panel_5.setBorder(new TitledBorder(null, "Constructor menu", TitledBorder.LEADING,
		TitledBorder.TOP, null, null));
	panel_5.setBounds(10, 590, 491, 74);
	contentPane.add(panel_5);
	panel_5.setLayout(null);

	textField_5 = new JTextField();
	textField_5.setText("5");
	textField_5.setBounds(10, 43, 86, 20);
	panel_5.add(textField_5);
	textField_5.setColumns(10);

	textField_6 = new JTextField();
	textField_6.setText("10");
	textField_6.setBounds(106, 43, 67, 20);
	panel_5.add(textField_6);
	textField_6.setColumns(10);

	JLabel lblVertexCount = new JLabel("Vertex count:");
	lblVertexCount.setBounds(10, 18, 86, 14);
	panel_5.add(lblVertexCount);

	JLabel lblEdgeCount = new JLabel("Edge count:");
	lblEdgeCount.setBounds(106, 18, 67, 14);
	panel_5.add(lblEdgeCount);

	final JToggleButton tglbtnTrue = new JToggleButton("true");
	tglbtnTrue.setSelected(true);
	tglbtnTrue.setBounds(183, 42, 67, 23);
	panel_5.add(tglbtnTrue);

	final JToggleButton tglbtnTrue_1 = new JToggleButton("true");
	tglbtnTrue_1.setSelected(true);
	tglbtnTrue_1.setBounds(260, 42, 154, 23);
	panel_5.add(tglbtnTrue_1);

	JLabel lblIsOrented = new JLabel("Is orented:");
	lblIsOrented.setBounds(183, 18, 67, 14);
	panel_5.add(lblIsOrented);

	JLabel lblIsMatrixModel = new JLabel("Is list model (else matrix model):");
	lblIsMatrixModel.setBounds(260, 18, 154, 14);
	panel_5.add(lblIsMatrixModel);

	JButton btnNewButton = new JButton("build");
	btnNewButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Random random = new Random();
		LayoutPanel lp = LayoutPanel.getInstance();
		Graph graph = Graph.getInstance();
		if (tglbtnTrue.isSelected()) {
		    graph.setGraphOrientation(GraphOrientation.ORIENTED_GRAPH);
		    rdbtnUndirected.setSelected(false);
		    rdbtnDirectedGraph.setSelected(true);
		} else {
		    graph.setGraphOrientation(GraphOrientation.UNORIENTED_GRAPH);
		    rdbtnUndirected.setSelected(true);
		    rdbtnDirectedGraph.setSelected(false);
		}
		if (tglbtnTrue_1.isSelected()) {
		    graph.convertToListGraph();
		    rdbtnList.setSelected(true);
		    rdbtnMatrix.setSelected(false);
		} else {
		    graph.convertToMatrixGraph();
		    rdbtnList.setSelected(false);
		    rdbtnMatrix.setSelected(true);
		}
		while (graph.getVertexCount() < Integer.parseInt(textField_5.getText())) {
		    boolean successful = true;
		    int vertexDataTemp = random.nextInt(100);
		    for (int i = 0; i < lp.ellipseArr.size(); i++) {
			if (lp.ellipseArr.get(i).data.toString().equals(
				String.valueOf(vertexDataTemp))) {
			    successful = false;
			}
		    }
		    if (successful) {
			lp.ellipseArr.add(new GraphVertexEllipse(
				random.nextInt(lp.getWidth() - 100) + 50, random.nextInt(lp
					.getHeight() - 100) + 50, LayoutPanel.DEFAULT_SIDE,
				LayoutPanel.DEFAULT_SIDE, String.valueOf(vertexDataTemp)));
			graph.addVertex(String.valueOf(vertexDataTemp));
		    }
		}
		if (graph.getGraphDirectionType() == GraphOrientation.ORIENTED_GRAPH) {
		    while (graph.getEdgeCount() < Integer.parseInt(textField_6.getText())) {
			int fromIndex = random.nextInt(graph.getVertexCount());
			int toIndex = random.nextInt(graph.getVertexCount());
			if (fromIndex == toIndex || graph.isEdgeExist(fromIndex, toIndex)) {
			    continue;
			} else {
			    graph.addEdge(fromIndex, toIndex, String.valueOf(random.nextInt(100)));
			    graph.setEdgeWeight(fromIndex, toIndex,
				    String.valueOf(random.nextInt(100)));
			    graph.setEdgeData(fromIndex, toIndex,
				    String.valueOf(random.nextInt(100)));
			}
		    }
		} else {
		    while (graph.getEdgeCount() < Integer.parseInt(textField_6.getText()) * 2) {
			int fromIndex = random.nextInt(graph.getVertexCount());
			int toIndex = random.nextInt(graph.getVertexCount());
			if (fromIndex == toIndex || graph.isEdgeExist(fromIndex, toIndex)) {
			    continue;
			} else {
			    graph.addEdge(fromIndex, toIndex, String.valueOf(random.nextInt(100)));
			    graph.setEdgeWeight(fromIndex, toIndex,
				    String.valueOf(random.nextInt(100)));
			    graph.setEdgeData(fromIndex, toIndex,
				    String.valueOf(random.nextInt(100)));
			}
		    }
		}
		lp.repaint();
	    }
	});
	btnNewButton.setBounds(423, 18, 58, 47);
	panel_5.add(btnNewButton);

	comboBoxStateChanged();
    }

    protected void comboBoxStateChanged() {
	String choice = (String) comboBox.getSelectedItem();
	if (choice.equals(IteratorKind.VERTEX_ITERATOR.toString())) {
	    iterator = new VertexIterator();
	    iterator.setToBegin();
	    Object data = iterator.getData();
	    if (data != null) {
		textField.setText(data.toString());
	    } else {
		textField.setText("null");
	    }
	    textField_2.setEnabled(false);
	    textField_3.setEnabled(false);

	} else if (choice.equals(IteratorKind.GRAPH_EDGE_ITERATOR.toString())) {
	    iterator = new GraphEdgeIterator();
	    iterator.setToBegin();
	    if (iterator.getData() != null) {
		String[] strArr = (String[]) iterator.getData();
		textField.setText(strArr[0]);
		textField_3.setText(strArr[1]);
	    } else {
		textField.setText("null");
		textField_3.setText("null");
	    }
	    textField_2.setEnabled(true);
	    textField_3.setEnabled(true);
	} else if (choice.equals(IteratorKind.GOING_OUT_EDGE_ITERATOR.toString())) {
	    String answer = JOptionPane
		    .showInputDialog("Enter index of certain vertex [0, VERTEX_COUNT]:");
	    if (answer != null) {
		int index = 0;
		try {
		    index = Integer.valueOf(answer);
		} catch (Exception e) {
		    System.out.println("Exception - bad input int format!");
		}
		if (index >= 0 && index < Graph.getInstance().getVertexCount()) {
		    selectedVertexIndex = index;
		}
	    } else {
		JOptionPane.showConfirmDialog(this,
			"0'th vertex was chosen as a default! (if it is exist of course)");
		selectedVertexIndex = 0;
	    }
	    iterator = new GoingOutEdgeIterator();
	    iterator.setToBegin();
	    Object data = iterator.getData();
	    if (data != null) {
		String[] strArr = (String[]) data;
		textField.setText(strArr[0]);
		textField_3.setText(strArr[1]);
	    } else {
		textField.setText("null");
		textField_3.setText("null");
	    }
	    textField_2.setEnabled(true);
	    textField_3.setEnabled(true);
	} else if (choice.equals(IteratorKind.INCOMING_EDGE_ITERATOR.toString())) {
	    String answer = JOptionPane
		    .showInputDialog("Enter index of certain vertex [0, VERTEX_COUNT]:");
	    if (answer != null) {
		int index = 0;
		try {
		    index = Integer.valueOf(answer);
		} catch (Exception e) {
		    System.out.println("Exception - bad input int format!");
		}
		if (index >= 0 && index < Graph.getInstance().getVertexCount()) {
		    selectedVertexIndex = index;
		}
	    } else {
		JOptionPane.showConfirmDialog(this,
			"0'th vertex was chosen as a default! (if it is exist of course)");
		selectedVertexIndex = 0;
	    }
	    iterator = new IncomingEdgeIterator();
	    iterator.setToBegin();
	    Object data = iterator.getData();
	    if (data != null) {
		String[] strArr = (String[]) data;
		textField.setText(strArr[0]);
		textField_3.setText(strArr[1]);
	    } else {
		textField.setText("null");
		textField_3.setText("null");
	    }
	    textField_2.setEnabled(true);
	    textField_3.setEnabled(true);
	}
	LayoutPanel.getInstance().repaint();
    }
}
